//
//  PrizeDetailViewController.m
//  happyHealthyWalk
//
//  Created by newhuidong on 16/2/26.
//  Copyright © 2016年 HuiDong. All rights reserved.
//
#define color(r,g,b,al) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:al]

#import "PrizeDetailViewController.h"
#import "GoodsTopView.h"
#import "GoodsMiddleView.h"
#import "GoodsBottomView.h"
#import "Header.h"
#import "MJRefresh.h"
#import "TableViewCell.h"
#import "ExchangeViewController.h"
@interface PrizeDetailViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,weak)GoodsTopView * topView;
@property (weak, nonatomic) UIScrollView *MyScrollView;
@property(nonatomic,weak)GoodsMiddleView * middleView;
@property(nonatomic,weak)GoodsBottomView * bottomView;
@property(nonatomic,weak)UITableView * detailTableView;

@property(nonatomic,strong)MJRefreshHeaderView * header;
@property(assign,nonatomic)float TopViewScale;

@property(nonatomic,weak)UIView * NavBarView;
@property(nonatomic,strong)UILabel * label;



@end

@implementation PrizeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.TopViewScale=1.0;
    
    self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
    
    
    self.title=@"商品详情";
    
    [self initView];
    [self addNavbarView];
    
}
-(void)addNavbarView{
    UIView * view=[[UIView alloc]init];
    self.NavBarView=view;
    view.frame=CGRectMake(0, 0, ScreenW, 64);
    _label=[[UILabel alloc]initWithFrame:CGRectMake(ScreenW/2-40, 25, 80, 30)];
    _label.text=@"商品详情";
    _label.textAlignment=NSTextAlignmentCenter;
    _label.textColor=[UIColor whiteColor];
    
    [view addSubview:_label];
    
    UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(20, 30, 15, 20);
    [button setBackgroundImage:[UIImage imageNamed:@"backTip"] forState:0];
    
    [button addTarget:self action:@selector(leftBarBtnPress) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:button];
    
    [self.view addSubview:view];
    
    
    
}
-(void)leftBarBtnPress{
    
    //[self.navigationController popoverPresentationController];
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}
-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden=YES;
    
}
-(void)viewWillDisappear:(BOOL)animated{
    
    
    self.navigationController.navigationBarHidden=NO;
    
    [self.header free];
    
    [self.MyScrollView removeFromSuperview];
    [super viewWillDisappear:animated];
    
    
}
-(UIScrollView*)MyScrollView{
    
    if (_MyScrollView==nil) {
        
        UIScrollView * scroll=[[UIScrollView alloc]init];
        _MyScrollView=scroll;
        scroll.delegate=self;
        scroll.frame=CGRectMake(0, 0, ScreenW, ScreenH-50);
        scroll.pagingEnabled=YES;
        scroll.showsHorizontalScrollIndicator=NO;
        scroll.tag=0;
        [self.view addSubview:scroll];
        
    }
    
    return _MyScrollView;
    
    
}
-(void)initBottomView{
    

}


-(void)initView{
    
    UIView * firstPageView=[[UIView alloc]init];
    
    firstPageView.frame=CGRectMake(0, 0, ScreenW, ScreenH-50);
    
    GoodsTopView * topView=[GoodsTopView view];
    
    self.topView=topView;
    
    topView.frame=CGRectMake(0, 0, ScreenW, 300);
    [firstPageView addSubview:topView];
    
    GoodsMiddleView * middleView=[GoodsMiddleView view];
    self.middleView=middleView;
    
    middleView.frame=CGRectMake(0, CGRectGetMaxX(topView.frame)+6, ScreenW, 195);
    [firstPageView addSubview:middleView];
    
    GoodsBottomView * bottomView=[GoodsBottomView view];
    self.bottomView=bottomView;
    CGFloat bottomViewY =self.MyScrollView.frame.size.height-40;
    bottomView.frame=CGRectMake(0, bottomViewY, ScreenW, 50);
    
    [firstPageView addSubview:bottomView];
    
    [self.MyScrollView addSubview:firstPageView];
    [self initBottomView];
    //初始化第二个页面
    [self addSecondPageTopTabBar];
    
    
    
    
    self.MyScrollView.contentSize=CGSizeMake(ScreenW, (ScreenH-50)*2);
    
}
-(void)addSecondPageTopTabBar{
    //初始化第二个页面的父亲view
    UIView* secondPageView = [[UIView alloc] init];
    secondPageView.frame = CGRectMake(0, ScreenH - 50, ScreenW, ScreenH - 50);
//    NSArray* array  = @[@"图文详情",@"宝贝评价",@"宝贝咨询"];
//    MyOrderTopTabBar* tabBar = [[MyOrderTopTabBar alloc] initWithArray:array] ;
//    tabBar.frame = CGRectMake(0,NaviBarH, screenW, TopTabBarH);
//    tabBar.backgroundColor = [UIColor whiteColor];
//    tabBar.delegate = self;
    //self.TopTabBar = tabBar;
   // [secondPageView addSubview:tabBar];
    //初始化一个UITableView
    UITableView* tableview = [[UITableView alloc] init];
    self.detailTableView = tableview;
    tableview.dataSource = self;
    tableview.delegate = self;
    tableview.tag = 1;
    tableview.separatorStyle=NO;
    
    tableview.frame = CGRectMake(0, 0, ScreenW,secondPageView.frame.size.height);
    MJRefreshHeaderView* RheaderView = [MJRefreshHeaderView header];
    RheaderView.scrollView = tableview;
    self.header = RheaderView;
    RheaderView.beginRefreshingBlock = ^(MJRefreshBaseView* refreshView){
        NSOperationQueue* Queue = [[NSOperationQueue alloc] init];
        [Queue addOperationWithBlock:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [UIView animateWithDuration:0.5 animations:^{
                    self.MyScrollView.contentOffset = CGPointMake(0, 0);
                } completion:^(BOOL finished) {
                    self.MyScrollView.scrollEnabled = YES;
                }];
                [self.header endRefreshing];
            });
        }];
    };
    
    [secondPageView addSubview:tableview];
    [self.MyScrollView addSubview:secondPageView];

    
}
#pragma -- <UIScrollViewDelegate>
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@" --== %f",scrollView.contentOffset.y);
    if(scrollView.tag == 0){
        if(scrollView.contentOffset.y<0){
            if(self.TopViewScale<1.01){
                self.TopViewScale += 0.00015f;
                [self.topView.icon_img setTransform:CGAffineTransformScale(self.topView.icon_img.transform, self.TopViewScale, self.TopViewScale)];
            }
            scrollView.contentOffset = CGPointMake(0, 0);
        }else{
            self.NavBarView.backgroundColor = color(0.0,162.0,154.0, scrollView.contentOffset.y/(ScreenH-50));
        }
        if(scrollView.contentOffset.y == (ScreenH-50)){
            scrollView.scrollEnabled = NO;
        }else if (scrollView.contentOffset.y == -64 && !scrollView.isDragging){
            [UIView animateWithDuration:0.3 animations:^{
                scrollView.contentOffset = CGPointMake(0, 0);
            }];
        }else;
    }
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    
    
    self.TopViewScale=1.0;
    
    [UIView animateWithDuration:0.5 animations:^{
       
        [self.topView.icon_img setTransform:CGAffineTransformIdentity];
        
        
        
    }];
    
    
    
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //NSLog(@" -- %f",scrollView.contentOffset.y);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString * identifer=@"TableViewCell";
    
    TableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell) {
        
        cell=[[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil] lastObject];
        
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return nil;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160;
    
}
@end
